package cn.itsource.basic.util;

import cn.itsource.basic.jwt.JwtUtil;
import cn.itsource.basic.jwt.Payload;
import cn.itsource.org.domain.TEmployee;
import cn.itsource.org.mapper.TEmployeeMapper;
import cn.itsource.user.domain.Logininfo;
import cn.itsource.user.domain.User;
import cn.itsource.user.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.servlet.http.HttpServletRequest;

@Component
public class LoginContext {

    @Autowired
    private TEmployeeMapper employeeMapper;
    @Autowired
    private UserMapper userMapper;

    public Object getLoginObject(HttpServletRequest request){
        //1.根据request获取请求头的jwtToken
        String token = request.getHeader("token");

        //2.解析jwtToken - payload - Logininfo
        Payload payload = JwtUtil.parseJwtToken(token);
        Logininfo logininfo = payload.getLogininfo();

        if(logininfo.getType() == 0){//管理员
            TEmployee employee = employeeMapper.selectByLogininfoId(logininfo.getId());
            return employee;
        }else{
            User user = userMapper.selectByLogininfoId(logininfo.getId());
            return user;
        }
    }
}
